class Surveys::CTestsManageController < ApplicationController
  #before_filter :auth
  # GET /tests
  # GET /tests.xml
  protect_from_forgery :except =>"create_question"
  layout "surveys/surveys"
  
  def index
    @tests = Survey.c_surveys(20,params[:page])
    respond_to do |format|
      format.html
    end
  end

  # 添加新的测试
  def new
    @test = Survey.new
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @test }
    end
  end
  #显示
  def show
    @test = Survey.find(params[:id])
  end
  
  #删除
  def destroy
    @test = Survey.find(params[:id])
    @test.deleted = true
    @test.save

    respond_to do |format|
      format.html { redirect_to(:action => "index") }
      format.xml  { head :ok }
    end
  end
  
    # 编辑
  def edit
    @test = Survey.find(params[:id])
  end

   def update
    @test = Survey.find(params[:survey][:id])
    @test.t_type=TEST_TYPE_COUNT
    respond_to do |format|
      if @test.update_attributes(params[:survey])
        flash[:notice] = '修改成功.'
        format.html { redirect_to :action => "index" }
      else
        format.html { render :action => "edit" }
      end
    end
  end
  
  def add_result
    @test = Survey.find(params[:id])
  end
  #创建新的测试
  def create
    @test = Survey.new(params[:survey])
    #设置测试类型为倾向测试
    @test.t_type = TEST_TYPE_COUNT
    @test.professional = false
    respond_to do |format|
      if @test.save       
        flash[:notice] = '测试添加成功.'
        format.html { redirect_to(:controller=> "c_tests_manage" ,:action => "index" ) }
        format.xml  { render :xml => @test, :status => :created, :location => @test }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @test.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  #添加题目
  def new_questions
    @test = Survey.find(params[:id])
    #生成一个空对象
    @question=Question.new
    @question.survey_id=@test.id
    respond_to do |format|
      format.html 
    end
  end
  
  def create_question
    @test = Survey.find(params[:test_id])
    q = Question.new(params[:question])
    q.survey = @test      
    q.save
    if params["choice.content"]
      i = 0;
      params["choice.content"].each do |content|
        choice = Choice.new(:content => content, :value => params["choice.value"][i])
        choice.question = q
        choice.save 
        i += 1
      end
    end
    flash[:notice] = 'Question添加成功.'
    respond_to do |format|
      format.html { render :partial => "questions"}
    end
  end 
  
  def update_question
    @test = Survey.find(params[:test_id])
    @question_num = params[:question_num]
    @question = Question.find(params[:id])
    @question.number=params[:question_number]
    @question.can_ignore = params[:can_ignore]
    @question.description = params["description"]
    begin
      @question.save!
      params["choice_ids"].each do |id|
        choice = Choice.find(id)
        choice.content = params["choice#{id}"]["content"]
        choice.value = params["choice#{id}"]["value"]
        choice.save
      end
      respond_to do |format|
        format.html { render :partial => "question"}
      end
    rescue
      flash[:q_error]=@question.errors[:number]
      respond_to do |format|
        format.html { render :partial => "question"}
      end
    end
  end

  #测试完成，可以上线
  def finish
    @test = Survey.find(params[:id])
    @test.status = TEST_STATUS_FINISHED
    @test.save
    respond_to do |format|
      format.html { redirect_to(:action => "index") }
    end
  end
  

  def destroy_question

    @question = Question.find(params[:id])
    @test = @question.survey

    @question.delete
    @question.choices.delete_all

    @hide_id = @question.id

    respond_to do |format|
      format.html { render :partial => "questions"}
    end
  end

 
  def new_norm
    @norm = Norm.new
    @test_id = params[:id]
    @test=Survey.find_by_id(@test_id)
  end
  
  def create_norm
    @test = Survey.find(params[:test_id])
    @norm = Norm.new(params[:norm])
    @norm.survey = @test
    respond_to do |format|
      if @norm.save       
        format.html { redirect_to(:action => "new_norm", :id => @test.id ) }
      else
        format.html { render :action => "new_norm" }
      end
    end
  end  

  def delete_norm
    @norm = Norm.find(params[:id])
    @norm.destroy
  
    respond_to do |wants|
      wants.html { redirect_to :action => 'new_norm', :id => @norm.survey.id }
    end
  end

end



